
import { _decorator, Component, Node, Sprite, EventTouch, UITransform, v3, Tween, tween, view } from 'cc';
import { CardGroup } from './CardGroup';
import { CardGroupParent } from './CardGroupParent';
import { SoltaireGame } from './SoltaireGame';
import GameManager from './tool/GameManager';
import { SoundEffect } from './tool/SoundEffect';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Card
 */
 
@ccclass('Card')
export class Card extends Component {

    @property(Node)
    bgkNode: Node= null!;

    @property(Sprite)
    diSprite: Sprite= null!;

    @property(Node)
    selected: Node= null!;

    cardData=1;// 001 ,百位代表花色，个十位代表
    cardNumber=1;
    cardColor=1;

    sceneWidth=320;
    cellSize=64;
    sizeWidth=640;
    start () {

        this.sizeWidth = view.getVisibleSize().width;
        if (SoltaireGame.instance.ScreenOrientation==1) {
            this.sceneWidth=320;
            this.cellSize=64;
        }else
        {
            this.sceneWidth=480;
            this.cellSize=96;
        }

        this.AddTouchEvent();
    }

    AddTouchEvent()
    {
        //this.node.on(Node.EventType.TOUCH_START, this.touchStartEvent, this);
        //this.node.on(Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.node.on(Node.EventType.TOUCH_END, this.touchEndEvent, this);
        //this.node.on(Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
    }

    RemaveTouchEvent()
    {
        //this.node.off(Node.EventType.TOUCH_START, this.touchStartEvent, this);
        //this.node.off(Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.node.off(Node.EventType.TOUCH_END, this.touchEndEvent, this);
        //this.node.off(Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
    }

    cardGroup:CardGroupParent=null;
    Init(cd:number,cg:CardGroupParent)
    {
        this.cardGroup=cg;
        this.cardData=cd;
        this.cardColor=Math.floor(cd/100);
        this.cardNumber=cd%100;
        //console.log(this.cardColor+"-"+this.cardNumber);
        this.diSprite.spriteFrame=SoltaireGame.instance.numberSF[(this.cardColor-1)*13+this.cardNumber-1];

    }

    isTouch=false;
    //翻开新卡牌
    MoveGroupTurnoverCard(dTime,end)
    {
        //console.log(end);
        this.bgkNode.scale=v3(0,1,1);
        this.diSprite.node.scale=v3(1,1,1);

        this.node.scale=v3(0,0,0);
        tween(this.node).delay(dTime).to(0.5+dTime, {scale:v3(1,1,1)},{easing:"cubicOut"}).start();

        // tween(this.bgkNode).delay(dTime).to(0.15, {scale:v3(0,1,1)}).start();
        // tween(this.diSprite.node).delay(dTime).to(0.15, {scale:v3(1,1,1)}).start();

        tween(this.node).delay(dTime).to(0.5+dTime, {position:end},{easing:"cubicOut"})
        .call(()=>{
        }).start()
    }

    MoveGroup(dTime,end)
    {
        this.node.scale=v3(0,0,0);
        tween(this.node).delay(dTime).to(0.5+dTime, {scale:v3(1,1,1)},{easing:"cubicOut"}).start();
        tween(this.node).delay(0.15).to(0.5+dTime, {position:end},{easing:"cubicOut"})
        .call(()=>{
        }).start()
    }

    ShowCard()
    {
        tween(this.bgkNode).to(0.15, {scale:v3(0,1,1)}).start();
        tween(this.diSprite.node).delay(0.15).to(0.15, {scale:v3(1,1,1)}).start();
    }

    SetShow()
    {
        this.bgkNode.scale=v3(0,1,1);
        this.diSprite.node.scale=v3(1,1,1);
    }

    //是否显示卡牌
    SetShowCard(_is:boolean)
    {
        if (_is) {
            this.bgkNode.scale=v3(0,1,1);
            this.diSprite.node.scale=v3(1,1,1);
        } else {
            this.bgkNode.scale=v3(1,1,1);
            this.diSprite.node.scale=v3(0,1,1);
        }
    }

    //重置父节点
    ResetParent(cg:CardGroup)
    {
        //this.cardGroup=cg;
        //this.node.setPosition(0,-65,0);

        //this.AddTouchEvent();
    }

    SetSelected(_is:boolean)
    {
        this.selected.active=_is;
    }

    touchStartParent=null;
    touchStartEvent(event: EventTouch) {
        //event.propagationStopped = false;
        if (this.isTouch==false) {
            return;
        }

    }

    touchMoveEvent(event: EventTouch) {
        if (this.isTouch==false) {
            return;
        }

    
    }

    isClick=false;
    touchEndEvent(event: EventTouch) {
        if (this.isTouch==false) {
            return;
        }

        //let location = event.touch.getUILocation();
        //let pos = this.cardGroup.node.parent.getComponent(UITransform)!.convertToNodeSpaceAR(v3(location.x, location.y, 0));

        let pos=this.node.position.clone();
        if (this.isClick) {
            this.isClick=false;
            this.selected.active=false;
            //SoltaireGame.instance.TouchWinInterchange(this,"remove");
            this.cardGroup.SelectCards(this,false);
        } else {
            this.isClick=true;
            this.node.setPosition(pos);
            this.selected.active=true;
            this.cardGroup.SelectCards(this,true);
        }
        
    }
    
    //交换强制选中
    InterchangeSelect()
    {
        let pos=this.node.position.clone();
        if (this.isClick) {
            this.isClick=false;
            this.selected.active=false;
        } else {
            this.isClick=true;
            this.selected.active=true;
        }
    }

    QuxiaoXuanZhong()
    {
        this.isClick=false;
        this.selected.active=false;
    }
}


